<?php
/**
 * WeConsult.php
 */
class Domain_WeConsult
{
    /**
     * 获取帖子列表
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function ConsultList($allParams){
        $model = new Model_WeConsult;
        $info = $model -> getConsultList($allParams);
        return $info;
    }

    /**
     * 获取帖子列表
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function ConsultDetail($allParams){
        $model = new Model_WeConsult;
        if(isset($allParams['type']) && $allParams['type'] == 1)
            $model -> consultNumAdd($allParams['id']);
        $info = $model -> getConsultDetail($allParams['id']);
        return $info;
    }

    /**
     * 获取帖子分类列表
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function ConsultClassList(){
        $model = new Model_WeConsultClass;
        $info = $model -> getConsultClassList();
        return $info;
    }

    /**
     * 获取用户帖子分类选择页面数据
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function userConsultClassList(){
        $model = new Model_WeConsultClass;
        $modelBind = new Model_WeConsultClassBind();
        if(isset(DI() ->user['id']))
            $bindClass = $modelBind ->getConsultClassBind(DI() ->user['id']);
        if(isset($bindClass) && $bindClass!=null && $bindClass!=[])
            $hasClass = $model -> getConsultClassListById($bindClass['consultClassId']);
        $info = $model -> getConsultClassList();
        $ret['hasClass'] = null;
        if(isset($hasClass) && $hasClass!=[])
            $ret['hasClass'] = $hasClass;
        $ret['allClass'] = $info;
        return $ret;
    }

    /**
     * 修改插入用户绑定
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function updateBindClass($data){
        $modelBind = new Model_WeConsultClassBind();
        $info= $modelBind ->updateConsultClassBind($data);
        return $info;
    }

    /**
     * 修改轮换板
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function updateBanner($data){
        $model = new Model_WeBanner();
        $info= $model ->updateBanner($data);
        return $info;
    }

    /**
     * 添加轮换板
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function insertBanner($data){

        $model = new Model_WeBanner();
        $info = $model -> insertBanner($data);
        return $info;
    }

    /**
     * 修改咨询
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function updateConsult($data){
        $model = new Model_WeConsult();
        $info= $model ->updateConsult($data);
        return $info;
    }

    /**
     * 添加咨询
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function insertConsult($data){
        $model = new Model_WeConsult();
        $info = $model -> insertConsult($data);
        return $info;
    }

    /**
     * 添加课程咨询
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function insertAppConsult($data){
        $modelAuthor = new Model_WeConsultAuthor();
        $modelClass = new Model_WeConsultClass();
        $hasClass = $modelClass -> getConsultClassListByName($data['className']);
        if($hasClass != null)
            $data['classId'] = $hasClass['id'];
        else{
            $classInfo = $modelClass ->insertConsultClass(['consultClassName'=>$data['className']]);
            $data['classId'] = $classInfo;
        }
        unset($data['className']);
//        $hasAuthor = $modelAuthor -> getAuthorByName($data['authorName']);
//        if($hasAuthor != null)
//            $data['authorId'] = $hasAuthor['id'];
//        else{
//            $authorInfo = $modelAuthor ->insertAuthor(['consultAuthorName'=>$data['authorName']]);
//            $data['authorId'] = $authorInfo;
//        }
//
//        unset($data['authorName']);
        $model = new Model_WeConsult();
        $info = $model -> insertConsult($data);
        return $info;
    }

    /**
     * 添加作者
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function insertConsultAuthor($data){
        $model = new Model_WeConsultAuthor();
        $info = $model -> insertAuthor($data);
        return $info;
    }

    /**
     * 删除作者
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function deleteConsultAuthor($data){
        $model = new Model_WeConsultAuthor();
        $info = $model -> deleteAuthor($data);
        return $info;
    }

    /**
     * 插入评论
     * @desc wjp 2017-1-1
     * @throws PhalApi_Exception_InternalServerError
     */
    public function insertComment($data){

        if($data['type'] == 1){
            $consultModel = new Model_WeConsult();
            $consult = $consultModel ->getConsultDetail($data['fid']);

            if($consult == null)
                return 0;
            $model = new Model_WeComment();
            $info = $model -> insertComment($data);
            return $info;
        }
        return 0;
    }

    /**
     * 删除评论
     * @desc wjp 2017-1-1
     * @throws PhalApi_Exception_InternalServerError
     */
    public function deleteComment($data){
        $model = new Model_WeComment();
        $commentData = $model -> getComment($data['id']);
        if($commentData == null || $commentData['status'] = 0)
            return 0;
//        if($commentData['userId']!=DI()->user['id'])
//            return 0;
        $info = $model -> deleteComment($data['id']);
        return $info;
    }

    /**
     * 获取帖子列表
     * @desc wjp 2017-12-26
     * @throws PhalApi_Exception_InternalServerError
     */
    public function CommentList($allParams){
        $model = new Model_WeComment;
        $info = $model -> getCommentList($allParams);
        return $info;
    }

    /**
     * 插入评论点赞
     * @desc wjp 2017-1-1
     * @throws PhalApi_Exception_InternalServerError
     */
    public function insertCommentLike($data){
        $model = new Model_WeComment();
        $commentData = $model -> getComment($data['fid']);
        if($commentData == null || $commentData['status'] = 0)
            return 0;
        $modelLike = new Model_WeCommentLike();
        $commentDataLike =  $modelLike -> getCommentLike($data['fid'],DI()->user['id']);
        if($commentDataLike != null)
            return 0;
        $info = $modelLike -> insertCommentLike($data);
        return $info;
    }

    /**
     * 删除评论点赞
     * @desc wjp 2017-1-1
     * @throws PhalApi_Exception_InternalServerError
     */
    public function deleteCommentLike($data){
        $modelLike = new Model_WeCommentLike();
        $commentDataLike =  $modelLike -> getCommentLike($data['fid'],DI()->user['id']);
        if($commentDataLike == null || $commentDataLike['userId']!=DI()->user['id'])
            return 0;
        $info = $modelLike -> deleteCommentLike($data['fid']);
        return $info;
    }

    /**
     * 新建推荐模板
     * @desc wjp 2017-1-4
     * @throws PhalApi_Exception_InternalServerError
     */
    public function insertRecommend($data){
        $domain = new Model_WeRecommend();
        $array = [];
        $array[] = [$data['id1'],$data['type1']];
        $array[] = [$data['id2'],$data['type2']];
        $array[] = [$data['id3'],$data['type3']];
        $json = json_encode($array);
        $userInfo = $domain -> insertRecommend($json);
        return $userInfo;
    }

    /**
     * 删除推荐模板
     * @desc wjp 2017-12-27
     * @throws PhalApi_Exception_InternalServerError
     */
    public function deleteRecommend($data){
        $domain = new Model_WeRecommend();
        $userInfo = $domain -> deleteRecommend($data['id']);
        return $userInfo;
    }

    /**
     * 推荐模板修改
     * @desc wjp 2017-12-27
     * @throws PhalApi_Exception_InternalServerError
     */
    public function updateRecommend($data){
        $domain = new Model_WeRecommend();
        $array = [];
        $array[] = [$data['id1'],$data['type1']];
        $array[] = [$data['id2'],$data['type2']];
        $array[] = [$data['id3'],$data['type3']];
        $json = json_encode($array);
        $userInfo = $domain -> updateRecommend($data['id'],$json);
        return $userInfo;
    }

    /**
     * 获取模板
     * @desc wjp 2017-12-27
     * @throws PhalApi_Exception_InternalServerError
     */
    public function getRecommend($id){
        $domain = new Model_WeRecommend();
        $modelConsult = new Model_WeConsult();
        $recommend = $domain -> getRecommend($id);
        if($recommend == null)
            return null;
        $json = json_decode($recommend['recommend']);
        if(!is_array($json)||!isset($json[0][0])||!isset($json[0][1]))
            return null;
        $array = [];
        foreach ($json as $item) {
            if($item[1] == 1){
                $recommedCach = $modelConsult ->getConsultDetail($item[0]);
                $recommedCach['recommendType'] =  $item[1];
                $array[] = $recommedCach;
            }
            if($item[1] == 2){
            }
            if($item[1] == 3){
            }
        }
        if($array == null)
            return null;
        return $array;
    }

}